package com.imooc.bilibili.result;

/**
 * 统一返回结果封装
 */
public class Result<T> {

    /**
     * 状态码
     */
    private String code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public Result(String code,String message) {
        this.code = code;
        this.message = message;
    }

    public Result(T data) {
        code = "200";
        message = "操作执行成功";
        this.data = data;
    }

    /**
     * 不需要返回数据给前端的场景
     * @return
     */
    public static Result<String> success() {
        return new Result<>(null);
    }

    /**
     * 对前端返回字符串类型相应结果
     * @param data
     * @return
     */
    public static Result<String> success(String data) {
        return new Result<>(data);
    }

    /**
     * 执行失败，无需返回数据
     * @return
     */
    public static Result<String> fail() {
        return new Result<>("201","操作执行失败");
    }

    /**
     * 分场景返回code和message
     * @return
     */
    public static Result<String> fail(String code,String message) {
        return new Result<>(code,message);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    };
}
